﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SalesSys.Comm.Utility;
using SalesSys.DAL.Models;
using SalesSys.Comm.BLL;
using System.Data;

namespace SalesSys.BLL
{
    public class CommodityBLL : BaseBLL<Commodity, Guid>
    {
        //public override int Create(Commodity entity)
        //{
        //    var result = base.Create(entity);
        //    CommodityRecordBLL recordBll = new CommodityRecordBLL();
        //    recordBll.Create(entity);
        //    return result;
        //}

        //public override int Edit(Commodity entity)
        //{
        //    var result = base.Edit(entity);
        //    CommodityRecordBLL recordBll = new CommodityRecordBLL();
        //    recordBll.Create(entity);
        //    return result;
        //}

        public Tuple<int, IEnumerable<Commodity>> GetList(Guid? supplierId, string name, string type2, int page = 1, int rows = 30)
        {
            var q = Rep.Entities;
            if (supplierId != null)
            {
                q = q.Where(p => p.Supplier.Id == supplierId);
            }
            if (!string.IsNullOrWhiteSpace(name))
            {
                q = q.Where(p => p.Name.Contains( name));
            }
            if (!string.IsNullOrWhiteSpace(type2))
            {
                q = q.Where(p => p.Standard.Contains(type2)||p.Format.Contains(type2));
            }
            var count = q.Count();
            IEnumerable<Commodity> list = q.OrderByDescending(p => p.Format).Paging(page, rows).ToArray();
            return Tuple.Create(count, list);
        }

        public Tuple<int, IEnumerable<Commodity>> GetList(Guid? supplierId, string searchKey, QueryInfo qinfo)
        {
            var q = Rep.Entities;
            if (supplierId != null)
            {
                q = q.Where(p => p.Supplier.Id == supplierId);
            }
            if (!string.IsNullOrWhiteSpace(searchKey))
            {
                q = q.Where(p => p.Brand == searchKey);
            }
            var count = q.Count();
            IEnumerable<Commodity> list = q.OrderByDescending(p => p.Format).Paging(qinfo.PageSize, qinfo.PageNum).ToArray();
            return Tuple.Create(count, list);
        }

        //public List<CommodityResult> Query(Guid supplierId, string type)
        //{
        //    var result = new List<CommodityResult>();
        //    var newArray = new CommodityDto[1];
        //    var commodityResult = new CommodityResult();
        //    var q = Rep.Entities;
        //    List<Commodity> list = q.Where(p => p.Supplier.Id == supplierId && p.Name == type).ToList();

        //    var goods = SysConfigBLL.GoodsClass;//型号列表
        //    string[] standardItem = null;
        //    foreach (var item in goods)
        //    {
        //        if (item.Item1.Equals(type))
        //        {
        //            standardItem = item.Item2;
        //            break;
        //        }
        //    }
        //    if (standardItem.Length == 0)
        //        return result;
        //    var formatList = list.GroupBy(p => p.Format).Select(p => p.Key).ToList();
        //    var s = "";
        //    var tf = false;
        //    var dto = new CommodityDto();
        //    foreach (var format in formatList)
        //    {
        //        commodityResult = new CommodityResult();
        //        newArray = new CommodityDto[standardItem.Length];
        //        commodityResult.Format = format;
        //        for (int i = 0; i < standardItem.Length; i++)
        //        {
        //            s = standardItem[i];
        //            tf = false;
        //            dto = null;
        //            foreach (var item in list)
        //            {
        //                if (item.Format.Equals(format) && item.Standard.Equals(s))
        //                {
        //                    dto = new CommodityDto();
        //                    dto.Id = item.Id;
        //                    dto.Price = item.Price;
        //                    dto.Amount = item.Amount;
        //                    newArray[i] = dto;
        //                    list.Remove(item);
        //                    tf = true;
        //                    break;
        //                }
        //            }
        //            if (!tf) {
        //                //dto.Standard = s;
        //                //dto.SupplierId = supplierId;
        //                //dto.Type = type;
        //                newArray[i] = dto;
        //            }
        //        }
        //        commodityResult.Commoditys = newArray;
        //        result.Add(commodityResult);
        //    }
        //    return result;
        //}

        /// <summary>
        /// 通过规格型号验证是否已经存在
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public bool CheckCommodity(Commodity commodity)
        {
            Commodity old = null;
            if (commodity.Id != null)
                old = Rep.Entities.FirstOrDefault(p => p.Name == commodity.Name && p.Standard == commodity.Standard && p.Format == commodity.Format && p.Id != commodity.Id);
            else
                old = Rep.Entities.FirstOrDefault(p => p.Name == commodity.Name && p.Standard == commodity.Standard && p.Format == commodity.Format);
            return old != null;
        }
    }
}
